<?php

/**
 * @author juanma
 * @copyright 2012
 */


class Sgit_Graficos_Parent
{
    
    function __construct($library)
    {
        include_once(APPLICATION_PATH."/library/jpgraph/jpgraph.php");
        
        $this->request= Zend_Controller_Front::getInstance()->getRequest();
        $this->view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');

        $this->width=  $this->request->getParam('w',700);
        $this->height= $this->request->getParam('h',$this->width * 0.6);

        $this->id_zona=       $this->request->getParam('idZona');
        $this->json=          $this->request->getParam('json',0);
        $this->start=         $this->request->getParam('start');
        $this->end=           $this->request->getParam('end');
        $this->limit=         $this->request->getParam('limit',18);
        $this->color=        '#a0a0c0';
        

    }
    
    protected function setValue($value)
    {
        if($this->json){
            if(!$value) return 'null';
            
        }else{
            if(!$value) return '';
        }
        
        return (float)$value;
       
    }
    protected function loadJpgraph($library)
    {
        include_once(APPLICATION_PATH."/library/jpgraph/jpgraph_$library.php");
    }
    
    protected function loadModel($model)
    {
        $modelo = 'Application_Model_' . $model;
        return new $modelo();

    }
    
    
    
    
    function _step($nLabel, $width)
    {
        $step = 1;
        if ($width <= 400) {
            if ($nLabel > 20) $step = 2;
            if ($nLabel > 40) $step = 3;
            if ($nLabel > 60) $step = 4;
            if ($nLabel > 80) $step = 5;
        } else {
            if ($nLabel > 40) $step = 2;
            if ($nLabel > 60) $step = 3;
            if ($nLabel > 80) $step = 4;
        }
        return $step;
    }

    
    
    function _graph($width, $height, $nombre = null, $cache = 14000)
    {
        
        if(is_null($nombre)) {
        	$cache = null;
        }else{
        	if (ETAG == true) $this->_etag($nombre);
        	if (CACHEIMAGENES == false){
        		$nombre=null;$cache=null;
        	} 
        }
        
    
        $graph = new Graph($width, $height, $nombre, $cache);
    
        // Adjust the Graph
        $graph->SetScale('textlin');
        $graph->SetFrame(false);
        $graph->SetMargin(75, 25, 30, 80);
        $graph->SetMarginColor('#FFFFFF');
    
    
        // Adjust the legend
        $graph->legend->SetLayout(LEGEND_HOR);
        $graph->legend->Pos(0, 0.99, "right", "bottom");
        $graph->legend->SetFillColor('#F3F3F3');
        $graph->legend->SetFont(FF_ARIAL, FS_NORMAL, 8);
        $graph->legend->SetShadow(false);
        $graph->legend->SetFrameWeight(0);
        $graph->legend->SetLeftMargin(0);
    
    
        //adjust de Y axis
        $graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD, 9);
        $graph->yaxis->SetTitlemargin(62);
        $graph->yaxis->SetLabelFormatCallback(array($this,'_yLabelFormatSinDec'));
        $graph->yaxis->scale->SetGrace(20);
        $graph->yaxis->SetTitleSide(SIDE_LEFT);
    
    
        //adjust de X axis
        $graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD, 11);
        $graph->xaxis->SetLabelAngle(90);
    
        //adjust the grid
        $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#EFEFEF@0.7');
        $graph->xgrid->Show(false);
    
    
        //adjust the background
       	$graph->SetBackgroundGradient('#FFFFFF@0.5', '#EBCCFF@0.5', GRAD_HOR, BGRAD_PLOT);
    
    
        return $graph;
    }

    
    function _etag($nombre)
    {
        $eTag = substr($nombre, 0, -4);
        header('Etag: ' . $eTag);
        header("Cache-Control: public");
        if (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $eTag)
        {
            header("HTTP/1.1 304 Not Modified");
            exit;
        }
    }

	
    function _linePlot($values, $legend = null, $color = 'red', $width = 1, $mark = true)
    {
        $lineplot = new LinePlot($values);
        $lineplot->SetColor($color);
        $lineplot->SetWeight($width);
        if ($mark) {
            $lineplot->mark->SetType(MARK_FILLEDCIRCLE);
            $lineplot->mark->SetFillColor($color);
            $lineplot->mark->SetWidth($width);
        }
        if (!is_null($legend)) $lineplot->SetLegend($legend);
    
        return $lineplot;
    }
    
    
    function _barPlot($values,$color = 'red', $width = 1.0, $showvalue = true)
    {
        $barplot = new BarPlot($values);
    	$barplot->SetColor($color);
    	$barplot->SetWidth($width);
    	$barplot->SetFillColor("$color@0.5");
    	if($showvalue){
    		$barplot->value->Show();
    		$barplot->value->SetFont(FF_ARIAL,FS_BOLD,7);
    		$barplot->value->SetFormat('%d');
    	}
    
        return $barplot;
    }
    
    
    
    function _yLabelFormatSinDec($aLabel)
    {
        return $this->view->numEs($aLabel, 0);
    }
    function _yLabelFormat($aLabel)
    {
        return $this->view->numEs($aLabel, 2);
    }
    function _yLabelFormat4($aLabel)
    {
        return $this->view->numEs($aLabel, 4);
    }



    
    
    
}